﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JohnnySins.Entities;
using Dapper;

namespace JohnnySins.DAL.Repositories
{
    public class MyVideoRepository : BaseRepository<MyVideo>
    {
        public IEnumerable<MyVideo> FindAll(bool? isPublic, bool? isFitness)
        {
            return Connection.Query<MyVideo>(@"SELECT mv.MyVideoId, mv.MyVideoTitle, mv.MyVideoFileName, mv.IsActive, mv.Poster, 
                        mv.HDWinFileName, mv.LDWinFileName, mv.HDQTFileName, mv.LDQTFileName,
                        mv.IsPublic, mv.IsFitness
                    FROM [MyVideo] (nolock) mv
                    where ((mv.IsPublic = @IsPublic) or (@IsPublic is null))
                        and ((mv.IsFitness = @IsFitness) or (@IsFitness is null))", new { IsPublic = isPublic, IsFitness = isFitness });
        }

        public IEnumerable<MyVideo> FindActive(bool isPublic, bool isFitness)
        {
            return Connection.Query<MyVideo>(@"SELECT mv.MyVideoId, mv.MyVideoTitle, mv.MyVideoFileName, mv.IsActive, mv.Poster, 
                        mv.HDWinFileName, mv.LDWinFileName, mv.HDQTFileName, mv.LDQTFileName,
                        mv.IsPublic, mv.IsFitness
                    FROM [MyVideo] (nolock) mv 
                    where (mv.IsActive = 1) and (mv.IsPublic = @IsPublic) and (mv.IsFitness = @IsFitness)
                    order by mv.MyVideoId desc", new { IsPublic = isPublic, IsFitness = isFitness });
        }

        public override MyVideo Find(int id)
        {
            return Connection.Query<MyVideo>(
                @"select MyVideoId, MyVideoTitle, MyVideoFileName, IsActive, Poster,
                    HDWinFileName, LDWinFileName, HDQTFileName, LDQTFileName,
                    IsPublic, IsFitness
                from [MyVideo]
                where MyVideoId = @MyVideoId", new { MyVideoId = id }).SingleOrDefault();
        }

        public override void Insert(MyVideo scene)
        {
            int result = Connection.Query<int>(
                @"INSERT INTO [MyVideo] (MyVideoTitle, MyVideoFileName, IsActive, Poster, HDWinFileName, LDWinFileName, HDQTFileName, LDQTFileName, IsPublic, IsFitness) 
                VALUES (@MyVideoTitle, @MyVideoFileName, @IsActive, @Poster, @HDWinFileName, @LDWinFileName, @HDQTFileName, @LDQTFileName, @IsPublic, @IsFitness); SELECT CAST(SCOPE_IDENTITY() AS INT);", scene).Single();
            scene.MyVideoId = result;
        }

        public override void Update(MyVideo scene)
        {
            Connection.Execute(
                @"update [MyVideo] set MyVideoTitle = @MyVideoTitle, IsActive = @IsActive, Poster = @Poster,
                  MyVideoFileName = @MyVideoFileName, HDWinFileName = @HDWinFileName, LDWinFileName = @LDWinFileName, HDQTFileName = @HDQTFileName, LDQTFileName = @LDQTFileName,
                    IsPublic = @IsPublic, IsFitness = @IsFitness 
                where MyVideoId = @MyVideoId", scene);
        }

        public void Activate(int id)
        {
            Connection.Execute(
                @"update [MyVideo] set IsActive = 1
                where MyVideoId = @MyVideoId", new { MyVideoId = id });
        }

        public void Deactivate(int id)
        {
            Connection.Execute(
                @"update [MyVideo] set IsActive = 0
                where MyVideoId = @MyVideoId", new { MyVideoId = id });
        }

        public override void Delete(int id)
        {
            Connection.Execute(
                @"delete from [MyVideo]
                    where MyVideoId = @MyVideoId", new { MyVideoId = id });
        }        
    }
}
